﻿using System;
using System.Configuration;
using System.ServiceModel.Activation;
using VNet.VisiSuite.BusinessLogic;
using VNet.VisiSuite.Common.Helper;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Enum;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Contact.Message.Security;
using VNet.VisiSuite.Service.Helper;
using VNet.VisiSuite.Service.Interface;

namespace VNet.VisiSuite.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class VNetVisiSuiteSecurityService : IVNetVisiSuiteSecurityService
    {
        private readonly SecurityUtil _businessLogic = new SecurityUtil();

        public SignInResponse SignIn(SignInRequest request)
        {
            var ip = IpAddressHelper.GetVisitorIpAddress();

            // Validate request without security
            request.RequiredNotNull();

            //validate registry InstakKey againts web.config
            if (request.InstallKey != ConfigurationManager.AppSettings["InstallKey"])
                return new SignInResponse { Success = false, Error = new ServiceError { ErrorCode = "Error InstallKey", Description = ErrorMessages.InstallKeyInvalid } };

            // Call to business layer to get data after sign in action
            return _businessLogic.SignIn(request);
        }

        public SignOutResponse SignOut(SignOutRequest request)
        {
            // Validate request with security
            request.ValidateInput("Sign Out");

            return _businessLogic.SignOut(request);
        }

        public GetDataResponse<LogoImageItem> DownloadLogoImage(GetDataRequest request)
        {
            //validate registry InstakKey againts web.config
            if (request.DataDescription != ConfigurationManager.AppSettings["InstallKey"])
                return new GetDataResponse<LogoImageItem> { Success = false, Error = new ServiceError { ErrorCode = "Error InstallKey", Description = ErrorMessages.InstallKeyInvalid } };

            var filePath = ConfigurationManager.AppSettings["LogoPath"];
            var logoImage = ImageHelper.GetLogoImageByUrl(filePath);

            return new GetDataResponse<LogoImageItem> { Success = true, Data = new LogoImageItem { FileName = filePath, Image = logoImage, }, };
        }
    }
}